﻿//
// App.xaml.cs
//
// Authors:
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Windows;
using System.Configuration;

using StructureMap;
using GalaSoft.MvvmLight;

namespace RSD
{
    using Data;
    using ViewModel;
    using View;
    using Scripting;
    using External;
    using Robot;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// Gets the System.Windows.Application object for the current System.AppDomain.
        /// </summary>
        /// <value>The System.Windows.Application object for the current System.AppDomain.</value>
        public static new App Current
        {
            get
            {
                return (App)Application.Current;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// </summary>
        /// <exception cref="T:System.InvalidOperationException">
        /// More than one instance of the <see cref="T:System.Windows.Application"/> 
        /// class is created per <see cref="T:System.AppDomain"/>.
        /// </exception>
        public App()
            : base()
        {
            InitializeComponent();
            InitializeDependencies();
        }

        /// <summary>
        /// Handles the Startup event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.StartupEventArgs"/> instance containing the event data.</param>
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (MainWindow == null)
            {
                MainWindow = ObjectFactory.GetInstance<EntryView>();
                MainWindow.Show();
            }
        }

        /// <summary>
        /// Initializes the application dependencies.
        /// </summary>
        private void InitializeDependencies()
        {
            ObjectFactory.Initialize(init =>
            {
                // Robot
                init.For<IRobotController>().Use<RobotController>();

                // DataContext
                init.For<IObjectContext>().Use(() => new RSDEntities());

                // Scripting
                init.For<IScorBotScript>().Use<ScorBotScript>();

                // Scale
                init.For<IScale>().Use(() => new Scale(ConfigurationManager.AppSettings["ComPort"]));

                //Repositories
                init.Scan(scan =>
                {
                    scan.AssemblyContainingType<IRepository>();
                    scan.WithDefaultConventions();
                });

                // ViewModels
                init.Scan(scan =>
                {
                    scan.AssemblyContainingType<ViewModelBase>();
                    scan.WithDefaultConventions();
                });

                // Views
                init.Scan(scan =>
                {
                    scan.AssemblyContainingType<Window>();
                    scan.WithDefaultConventions();
                });
            });
        }
    }
}
